package com.bsoft.gol.hcs.clinical.service.impl;

import com.bsoft.gol.hcs.clinical.bean.req.*;
import com.bsoft.gol.hcs.clinical.bean.response.BaseTcmDiseasesTotalResponse;
import com.bsoft.gol.hcs.clinical.bean.response.BaseWmDiseasesTotalResponse;
import com.bsoft.gol.hcs.clinical.bean.response.DrugInformationTotalResponse;
import com.bsoft.gol.hcs.clinical.bean.response.QueryDrugInformationResponse;
import com.bsoft.gol.hcs.clinical.bean.vo.BaseDrugVo;
import com.bsoft.gol.hcs.clinical.dao.*;
import com.bsoft.gol.hcs.clinical.service.IStandardDrugService;
import com.bsoft.gol.hcs.common.service.BaseDataService;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.annotation.RpcService;
import hcn.base.drug.BaseCurrencyDrug;
import hcn.base.drug.BaseDrug;
import hcn.base.drug.BaseFactory;
import hcn.base.drug.qo.DrugQo;
import hcn.base.drug.vo.StandardDrugVo;
import hcn.base.reviewdrug.FootNote;
import hcn.util.BaseResponse;
import hcn.service.commonClinical.qo.ParamsOrgQo;
import hcn.util.ResultCode;
import com.bsoft.gol.hcs.his.service.IElectronicMedicalHisService;
import opensource.jpinyin.PinyinHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import pcn.consultation.OdsDiagnosis;
import pcn.diagnosis.BaseTcmDiseases;
import pcn.diagnosis.BaseWmDiseases;
import com.bsoft.gol.hcs.satisfaction.bean.vo.DictionaryVo;
import com.bsoft.gol.hcs.utils.*;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 药品目录服务实现
 * @Author: zhaorb
 * @Date: 2019/12/31 11:10
 **/
@SsdevService("standardDrugService")
public class StandardDrugService implements IStandardDrugService {
    private static Logger log = LoggerFactory.getLogger(StandardDrugService.class);

    @Autowired
    private BaseCurrencyDrugDAO baseCurrencyDrugDAO;
    @Autowired
    private BaseDrugDAO baseDrugDAO;
    @Autowired
    private BaseFactoryDAO baseFactoryDao;
    @Autowired
    BaseTcmDiseasesDAO baseTcmDiseasesDAO;
    @Autowired
    BaseWmDiseasesDAO baseWmDiseasesDAO;
    @Autowired
    OdsDiagnosisDAO odsDiagnosisDAO;
    @Autowired
    FootNoteDAO footNoteDAO;
    @Autowired
    private IElectronicMedicalHisService electronicMedicalHisService;
    @Autowired
    private BaseDataService baseDataService;
    @Value("${project_source}")
    private String projectSource;

    /**
     * @Description: 获取本地药品信息
     * @Author: zhaorb
     * @Date: 2020/1/7 14:52
     **/
    @Override
    public BaseResponse<DrugInformationTotalResponse> queryLocalDrugInformation(QueryDrugInformationRequest request) {
        log.info("StandardDrugService.queryDrugInformation入参：{}", request);
        BaseResponse result = new BaseResponse();
        try {
            List<BaseDrugVo> list = baseDrugDAO.queryLocalDrugInformation(request);
            BigInteger total = baseDrugDAO.queryLocalDrugInformationTotal(request);
            List<QueryDrugInformationResponse> responseList = new ArrayList<>();
            for (BaseDrugVo vo : list) {
                QueryDrugInformationResponse response = new QueryDrugInformationResponse();
                BeanUtils.copyProperties(vo, response);
                response.setUnit(vo.getPackUnit());
                response.setDrugId(vo.getDrugId().toString());
                response.setOrgDrugId(vo.getOrgDrugId());
                response.setFactoryId(vo.getOrgFactoryId());
                response.setSpecifications(vo.getSpecification());
                response.setFactoryName(vo.getOrgFactoryName());
                response.setDrugDose(Double.valueOf(vo.getDose()));
                response.setPrice(Double.valueOf(vo.getPrice().toString()));
                responseList.add(response);
            }
            DrugInformationTotalResponse response = new DrugInformationTotalResponse();
            response.setList(responseList);
            response.setTotal(total);
            result.setData(response);
            result.setCode(ResultCode.SUCCESS);
        } catch (Exception e) {
            result.setCode(ResultCode.ERROR);
        }
        log.info("StandardDrugService.queryDrugInformation出参：{}", result);
        return result;
    }

    @Override
    public BaseResponse<DrugInformationTotalResponse> queryHisDrugInformation(QueryDrugInformationRequest request) {
//        return electronicMedicalHisService.queryHisDrugInformation(request);
        return electronicMedicalHisService.queryHisPlateformDrugInformation(request);
    }

    /**
     * @Description: 查询西医诊断疾病表
     * @Author: zhaorb
     * @Date: 2020/4/14 13:51
     **/
    @Override
    public BaseResponse<BaseWmDiseasesTotalResponse> findWesternMedicineDiagnosis(BaseWmDiseasesRequest request) {
        BaseResponse<BaseWmDiseasesTotalResponse> result = new BaseResponse();
        BaseWmDiseasesTotalResponse response = new BaseWmDiseasesTotalResponse();
        try {
            BigInteger total = baseWmDiseasesDAO.queryWesternMedicineDiagnosisTotal(request);
            List<BaseWmDiseases> list = baseWmDiseasesDAO.queryWesternMedicine(request);
            response.setList(list);
            response.setTotal(total);
            result.setCode(ResultCode.SUCCESS);
            result.setData(response);
            result.setMessage("查询西医诊断疾病表成功");
            log.info("查询西医诊断疾病表成功");

        } catch (Exception e) {
            result.setCode(ResultCode.ERROR);
            result.setMessage("查询西医诊断疾病表出错");
            log.error("查询西医诊断疾病表出错");
        }
        return result;
    }

    /**
     * @Description: 新增西医诊断疾病表
     * @Author: zhaorb
     * @Date: 2020/4/14 13:51
     **/
    @Override
    public BaseResponse insertWesternMedicineDiagnosis(BaseWmDiseasesRequest request) {
        BaseResponse result = new BaseResponse();
        result = filterCondition(result, request);
        List<BaseWmDiseases> list = checkExist(request);
        List<BaseWmDiseases> list2 = checkExistDiseasesCode(request);
        if (CommonUtils.isNotEmpty(list)) {
            result.setCode(ResultCode.ERROR);
            result.setMessage("西医疾病名称已存在");
            return result;
        }
        if (CommonUtils.isNotEmpty(list2)) {
            result.setCode(ResultCode.ERROR);
            result.setMessage("西医疾病代码已存在");
            return result;
        }
        try {
            BaseWmDiseases diseases = new BaseWmDiseases();
            BeanUtils.copyProperties(request, diseases);
            if (CommonUtils.isNotEmpty(request.getDiseasesName())) {
                diseases.setPinyinCode(PinyinHelper.getShortPinyin(request.getDiseasesName()));
            }
            //获得数据库中编号最大的diseasesId
            BaseWmDiseases baseWmDiseases = baseWmDiseasesDAO.getMaxBaseWmDiseases();
            if (CommonUtils.isNotEmpty(baseWmDiseases)) {
                diseases.setDiseasesId(baseWmDiseases.getDiseasesId() + 1);
                diseases.setDeleteFlag("0");
                baseWmDiseasesDAO.save(diseases);
            }
            result.setCode(ResultCode.SUCCESS);
            result.setMessage("新增成功");
            log.info("新增西药诊断{}成功", diseases.getDiseasesName());
        } catch (Exception e) {
            result.setCode(ResultCode.ERROR);
            result.setMessage("新增出错");
            log.error("新增西药诊断报错");
        }
        return result;
    }

    /**
     * @Description: 修改西医诊断疾病表
     * @Author: zhaorb
     * @Date: 2020/4/14 13:51
     **/
    @Override
    public BaseResponse updateWesternMedicineDiagnosis(BaseWmDiseasesRequest request) {
        BaseResponse result = new BaseResponse();
        result = filterCondition(result, request);
        List<BaseWmDiseases> list = checkExist(request);
        List<BaseWmDiseases> list2 = checkExistDiseasesCode(request);
        if (CommonUtils.isNotEmpty(list) && list.get(0).getDiseasesId() != request.getDiseasesId()) {
            result.setCode(ResultCode.ERROR);
            result.setMessage("西医疾病名称已存在");
            return result;
        }
        if (CommonUtils.isNotEmpty(list2) && list2.get(0).getDiseasesId() != request.getDiseasesId()) {
            result.setCode(ResultCode.ERROR);
            result.setMessage("西医疾病代码已存在");
            return result;
        }
        BaseWmDiseases baseWmDiseases = baseWmDiseasesDAO.getBaseWmDiseases(request.getDiseasesId());
        try {
            if (CommonUtils.isNotEmpty(baseWmDiseases)) {
                BaseWmDiseases diseases = new BaseWmDiseases();
                BeanUtils.copyProperties(request, diseases);
                if (CommonUtils.isNotEmpty(request.getDiseasesName())) {
                    diseases.setPinyinCode(PinyinHelper.getShortPinyin(request.getDiseasesName()));
                }
                baseWmDiseasesDAO.update(diseases);
                result.setCode(ResultCode.SUCCESS);
                result.setMessage("修改成功");
                log.info("修改西药诊断{}成功", diseases.getDiseasesName());
            } else {
                result.setCode(ResultCode.ERROR);
                result.setMessage("西医诊断不存在");
                return result;
            }
        } catch (Exception e) {
            result.setCode(ResultCode.ERROR);
            result.setMessage("修改出错");
            log.error("修改西药诊断报错");
        }
        return result;
    }

    /**
     * @Description: 删除西医诊断疾病表
     * @Author: zhaorb
     * @Date: 2020/4/14 13:51
     **/
    @Override
    public BaseResponse deleteWesternMedicineDiagnosis(BaseWmDiseasesRequest request) {
        BaseResponse result = new BaseResponse();
        //判断诊断是否使用
        List<OdsDiagnosis> list = odsDiagnosisDAO.queryOdsDiagnosis(request.getDiseasesId());
        if (CommonUtils.isNotEmpty(list)) {
            result.setCode(ResultCode.ERROR);
            result.setMessage("诊断已使用，不能删除！");
            return result;
        }
        BaseWmDiseases wmDiseases = baseWmDiseasesDAO.getBaseWmDiseases(request.getDiseasesId());
        try {
            if (CommonUtils.isNotEmpty(wmDiseases)) {
                //删除操作
                wmDiseases.setDiseasesId(request.getDiseasesId());
                wmDiseases.setDeleteFlag("1");
                baseWmDiseasesDAO.update(wmDiseases);
                result.setCode(ResultCode.SUCCESS);
                result.setMessage("删除成功");
                log.info("删除西药诊断{}成功", wmDiseases.getDiseasesName());
            } else {
                result.setCode(ResultCode.ERROR);
                result.setMessage("西医诊断不存在");
                return result;
            }
        } catch (Exception e) {
            result.setCode(ResultCode.ERROR);
            result.setMessage("删除出错");
            log.error("修改西药诊断报错");
        }
        return result;
    }


    private BaseResponse filterCondition(BaseResponse result, BaseWmDiseasesRequest request) {
        if (CommonUtils.isEmpty(request.getDiseasesName())) {
            result.setCode(ResultCode.ERROR);
            result.setMessage("西医诊断名称不存在");
            return result;
        }
        if (CommonUtils.isEmpty(request.getDiseasesCode())) {
            result.setCode(ResultCode.ERROR);
            result.setMessage("ICD码不存在");
            return result;
        }
        return new BaseResponse();
    }

    private List<BaseWmDiseases> checkExist(BaseWmDiseasesRequest request) {
        request.setDiseasesCode(null);
        request.setPinyinCode(null);
        return baseWmDiseasesDAO.queryWesternMedicine(request);
    }

    private List<BaseWmDiseases> checkExistDiseasesCode(BaseWmDiseasesRequest request) {
        request.setDiseasesName(null);
        request.setPinyinCode(null);
        return baseWmDiseasesDAO.queryWesternMedicine(request);
    }

    /**
     * @Description: 查询中医诊断疾病表
     * @Author: zhaorb
     * @Date: 2020/4/14 13:51
     **/
    @Override
    public BaseResponse<BaseTcmDiseasesTotalResponse> findChineseMedicineDiagnosis(BaseTcmDiseasesRequest request) {
        BaseResponse<BaseTcmDiseasesTotalResponse> result = new BaseResponse();
        try {
            BaseTcmDiseasesTotalResponse response = new BaseTcmDiseasesTotalResponse();
            BigInteger total = baseTcmDiseasesDAO.queryDiagnosticListTotal(request);
            List<BaseTcmDiseases> list = baseTcmDiseasesDAO.queryBaseTcmDiseases(request);
            response.setList(list);
            response.setTotal(total);
            result.setCode(ResultCode.SUCCESS);
            result.setData(response);
            result.setMessage("查询中医诊断疾病表成功");
            log.info("查询中医诊断疾病表成功");

        } catch (Exception e) {
            result.setCode(ResultCode.ERROR);
            result.setMessage("查询中医诊断疾病表出错");
            log.error("查询中医诊断疾病表出错");
        }
        return result;
    }

    @Override
    public BaseResponse<List<FootNote>> getDrugFootNote(QueryDiagnosticListRequest request) {
        log.info("WebCloudClinicServiceImpl.getDrugFootNote：{}", request);
        BaseResponse<List<FootNote>> baseResponse = new BaseResponse<>();
//        if (electronicMedicalService.getPath(request.getHospitalCode())) {
        try {
            List<FootNote> footNotes = footNoteDAO.queryFootNote(request.getHospitalCode());
            baseResponse.setCode(ResultCode.SUCCESS);
            baseResponse.setMessage("查询成功");
            baseResponse.setData(footNotes);
        } catch (Exception e) {
            baseResponse.setCode(ResultCode.ERROR);
            baseResponse.setMessage("查询失败");
        }
//        } else {
//            if (InterfaceTypeEnum.GOL.getCode().equals(projectSource)) {
//                Map golOrgParams = getGOLOrgParams(request.getHospitalCode());
//                if(null != golOrgParams){
//                    request.setHospitalCode((String) golOrgParams.get("organizationId"));
//                    request.setHisUrl((String) golOrgParams.get("hisUrl"));
//                }
//            }
//            BaseRequest baseRequest = new BaseRequest();
//            baseRequest.setHospitalCode(request.getHospitalCode());
//            baseResponse = electronicMedicalService.getHisDrugFootNote(baseRequest);
//        }
        return baseResponse;
    }

    @Override
    public BaseResponse saveDrugFootNote(FootNoteRequest request) {
        log.info("WebCloudClinicServiceImpl.saveDrugFootNote：{}", request);
        BaseResponse baseResponse = new BaseResponse();
        FootNote footNote = new FootNote();
        footNote.setFootNoteCode(request.getFootNoteCode());
        footNote.setFootNoteName(request.getFootNoteName());
        footNote.setPingyinCode(request.getPingyinCode());
        footNote.setRemark(request.getRemark());
        footNote.setHospitalCode(request.getHospitalCode());
        footNote.setFootNoteType((byte) 1);
        try {
            List<FootNote> footNotes = footNoteDAO.queryFootNote(request.getHospitalCode());
            if (CommonUtils.isNotEmpty(footNotes)) {
                if (CommonUtils.isNotEmpty(request.getFootNoteId())) {
                    footNote.setFootNoteId(request.getFootNoteId());
                    footNote.setFootNoteStatus(request.getFootNoteStatus());
                    footNoteDAO.update(footNote);
                } else {
                    baseResponse.setCode(ResultCode.ERROR);
                    baseResponse.setMessage("药品脚注存在");
                    return baseResponse;
                }
            } else {
                footNote.setFootNoteId(Utils.getUUID());
                footNoteDAO.save(footNote);
            }
            baseResponse.setCode(ResultCode.SUCCESS);
            baseResponse.setMessage("新增成功");
        } catch (Exception e) {
            baseResponse.setCode(ResultCode.ERROR);
            baseResponse.setMessage("新增失败");
        }
        return baseResponse;
    }

    private BaseResponse filterConditionChineseMedicine(BaseResponse result, BaseTcmDiseasesRequest request) {
        if (CommonUtils.isEmpty(request.getDiseasesName())) {
            result.setCode(ResultCode.ERROR);
            result.setMessage("中医诊断名称不存在");
            return result;
        }
        if (CommonUtils.isEmpty(request.getDiseasesCode())) {
            result.setCode(ResultCode.ERROR);
            result.setMessage("ICD码不存在");
            return result;
        }
        return new BaseResponse();
    }

    /**
     * @Description: 修改中医诊断疾病表
     * @Author: zhaorb
     * @Date: 2020/4/14 13:51
     **/
    @Override
    public BaseResponse updateChineseMedicineDiagnosis(BaseTcmDiseasesRequest request) {
        BaseResponse result = new BaseResponse();
        result = filterConditionChineseMedicine(result, request);
        List<BaseTcmDiseases> list = checkExistChineseMedicine(request);
        List<BaseTcmDiseases> list1 = checkExistChineseMedicineCode(request);
        if (CommonUtils.isNotEmpty(list) && list.get(0).getTcmDiseasesId() != request.getTcmDiseasesId()) {
            result.setCode(ResultCode.ERROR);
            result.setMessage("中医疾病名称已存在");
            return result;
        }
        if (CommonUtils.isNotEmpty(list1) && list1.get(0).getTcmDiseasesId() != request.getTcmDiseasesId()) {
            result.setCode(ResultCode.ERROR);
            result.setMessage("中医疾病代码已存在");
            return result;
        }
        BaseTcmDiseases baseTcmDiseases = baseTcmDiseasesDAO.getBaseTcmDiseases(request.getTcmDiseasesId());
        try {
            if (CommonUtils.isNotEmpty(baseTcmDiseases)) {
                BaseTcmDiseases diseases = new BaseTcmDiseases();
                BeanUtils.copyProperties(request, diseases);
                if (CommonUtils.isNotEmpty(request.getDiseasesName())) {
                    diseases.setPinyinCode(PinyinHelper.getShortPinyin(request.getDiseasesName()));
                    diseases.setWubiCode(WuBiUtil.getWBCode(request.getDiseasesName()));
                }
                baseTcmDiseasesDAO.update(diseases);
                result.setCode(ResultCode.SUCCESS);
                result.setMessage("修改成功");
                log.info("修改中药诊断{}成功", diseases.getDiseasesName());
            } else {
                result.setCode(ResultCode.ERROR);
                result.setMessage("中医诊断不存在");
                return result;
            }
        } catch (Exception e) {
            result.setCode(ResultCode.ERROR);
            result.setMessage("修改出错");
            log.error("修改中药诊断报错");
        }
        return result;
    }

    /**
     * @Description: 删除中医诊断疾病表
     * @Author: zhaorb
     * @Date: 2020/4/14 13:51
     **/
    @Override
    public BaseResponse deleteChineseMedicineDiagnosis(BaseTcmDiseasesRequest request) {
        BaseResponse result = new BaseResponse();
        //判断诊断是否使用
        List<OdsDiagnosis> list = odsDiagnosisDAO.queryOdsDiagnosis(request.getTcmDiseasesId());
        if (CommonUtils.isNotEmpty(list)) {
            result.setCode(ResultCode.ERROR);
            result.setMessage("诊断已使用，不能删除！");
            return result;
        }
        BaseTcmDiseases tcmDiseases = baseTcmDiseasesDAO.getBaseTcmDiseases(request.getTcmDiseasesId());
        try {
            if (CommonUtils.isNotEmpty(tcmDiseases)) {
                //删除操作
                tcmDiseases.setTcmDiseasesId(request.getTcmDiseasesId());
                tcmDiseases.setDeleteFlag("1");
                baseTcmDiseasesDAO.update(tcmDiseases);
                result.setCode(ResultCode.SUCCESS);
                result.setMessage("删除成功");
                log.info("删除中药诊断{}成功", tcmDiseases.getDiseasesName());
            } else {
                result.setCode(ResultCode.ERROR);
                result.setMessage("中医诊断不存在");
                return result;
            }
        } catch (Exception e) {
            result.setCode(ResultCode.ERROR);
            result.setMessage("删除出错");
            log.error("修改中药诊断报错");
        }
        return result;
    }

    /**
     * @Description: 新增中医诊断疾病表
     * @Author: zhaorb
     * @Date: 2020/4/14 13:51
     **/
    @Override
    public BaseResponse insertChineseMedicineDiagnosis(BaseTcmDiseasesRequest request) {
        BaseResponse result = new BaseResponse();
        result = filterConditionChineseMedicine(result, request);
        List<BaseTcmDiseases> tcmDiseases = checkExistChineseMedicine(request);
        List<BaseTcmDiseases> tcmDiseases2 = checkExistChineseMedicineCode(request);
        if (CommonUtils.isNotEmpty(tcmDiseases)) {
            result.setCode(ResultCode.ERROR);
            result.setMessage("中医疾病名称已存在");
            return result;
        }
        if (CommonUtils.isNotEmpty(tcmDiseases2)) {
            result.setCode(ResultCode.ERROR);
            result.setMessage("中医疾病代码已存在");
            return result;
        }
        try {
            BaseTcmDiseases diseases = new BaseTcmDiseases();
            BeanUtils.copyProperties(request, diseases);
            if (CommonUtils.isNotEmpty(request.getDiseasesName())) {
                diseases.setPinyinCode(PinyinHelper.getShortPinyin(request.getDiseasesName()));
                diseases.setWubiCode(WuBiUtil.getWBCode(request.getDiseasesName()));
            }
            //获得数据库中编号最大的tcm_diseases_id
            BaseTcmDiseases baseTcmDiseases = baseTcmDiseasesDAO.getMaxBaseTcmDiseases();
            if (CommonUtils.isNotEmpty(baseTcmDiseases)) {
                diseases.setTcmDiseasesId(baseTcmDiseases.getTcmDiseasesId() + 1);
                diseases.setDeleteFlag("0");
                baseTcmDiseasesDAO.save(diseases);
            }
            result.setCode(ResultCode.SUCCESS);
            result.setMessage("新增成功");
            log.info("新增中药诊断{}成功", diseases.getDiseasesName());
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(ResultCode.ERROR);
            result.setMessage("新增出错");
            log.error("新增中药诊断报错");
        }
        return result;
    }

    private List<BaseTcmDiseases> checkExistChineseMedicine(BaseTcmDiseasesRequest request) {
        request.setDiseasesCode(null);
        request.setPinyinCode(null);
        return baseTcmDiseasesDAO.queryBaseTcmDiseases(request);
    }

    private List<BaseTcmDiseases> checkExistChineseMedicineCode(BaseTcmDiseasesRequest request) {
        request.setDiseasesName(null);
        request.setPinyinCode(null);
        return baseTcmDiseasesDAO.queryBaseTcmDiseases(request);
    }

    /**
     * 获取诊断字典
     *
     * @param type
     * @return
     */
    @Deprecated
    public List<DictionaryVo> getBaseDiseasesList(String type, String pinyinCode, Integer pageNo, Integer pageSize) {
        List<DictionaryVo> dictionaryVos = new ArrayList<>();
        //西医
        if ("1".equals(type)) {
            BaseWmDiseasesRequest baseWmDiseasesRequest = new BaseWmDiseasesRequest();
            baseWmDiseasesRequest.setPinyinCode(pinyinCode);
            baseWmDiseasesRequest.setPageNo(pageNo);
            baseWmDiseasesRequest.setPageSize(pageSize);
            List<BaseWmDiseases> baseWmDiseasesList = baseWmDiseasesDAO.findWesternMedicine(baseWmDiseasesRequest);
            if (CommonUtils.isNotEmpty(baseWmDiseasesList)) {
                for (BaseWmDiseases baseWmDiseases : baseWmDiseasesList) {
                    DictionaryVo dictionaryVo = new DictionaryVo();
                    dictionaryVo.setDicCode(baseWmDiseases.getDiseasesCode());
                    dictionaryVo.setDicName(baseWmDiseases.getDiseasesName());
                    dictionaryVos.add(dictionaryVo);
                }
            }
        } else if ("2".equals(type)) {
            BaseTcmDiseasesRequest baseTcmDiseasesRequest = new BaseTcmDiseasesRequest();
            baseTcmDiseasesRequest.setPinyinCode(pinyinCode);
            baseTcmDiseasesRequest.setPageNo(pageNo);
            baseTcmDiseasesRequest.setPageSize(pageSize);
            List<BaseTcmDiseases> baseTcmDiseasesList = baseTcmDiseasesDAO.findBaseTcmDiseases(baseTcmDiseasesRequest);
            if (CommonUtils.isNotEmpty(baseTcmDiseasesList)) {
                for (BaseTcmDiseases baseTcmDiseases : baseTcmDiseasesList) {
                    DictionaryVo dictionaryVo = new DictionaryVo();
                    dictionaryVo.setDicCode(baseTcmDiseases.getDiseasesCode());
                    dictionaryVo.setDicName(baseTcmDiseases.getDiseasesName());
                    dictionaryVos.add(dictionaryVo);
                }
            }
        }

        return dictionaryVos;
    }

    /**
     * @description: 查询健康通的机构列表信息
     * @author: chenJij@bsoft.com.cn
     * @date: 2020/10/29 15:05
     */
    public Map getGOLOrgParams(String organizationId) {
        ParamsOrgQo qo = new ParamsOrgQo();
        qo.setOrganizationId(organizationId);
        List<Map> maps = (List<Map>) baseDataService.queryOrganizationList(qo).getData();
        if (CommonUtils.isNotEmpty(maps)) {
            for (Map map : maps) {
                if (Constants.GOL.equals(projectSource)) {
                    return map;
                }
            }
        }
        return null;
    }

    /**
     * 药品搜索
     *
     * @param drugQo
     * @return
     */
    @Override
    @RpcService
    public Map<String, Object> findStandardDrugList(DrugQo drugQo) {
        Map<String, Object> map = new HashMap<>();
        map.put("list", baseCurrencyDrugDAO.findStandardDrugList(drugQo));
        map.put("total", baseCurrencyDrugDAO.getStandardDrugCount(drugQo));
        return map;
    }

    /**
     * 启用/禁用
     *
     * @param drugId
     * @param s
     */
    @Override
    @RpcService
    public void openDrugStatus(Integer drugId, String s) {
        BaseDrug baseDrug = baseDrugDAO.get(drugId);
        baseDrug.setStatus(s);
        baseDrugDAO.update(baseDrug);
    }

    /**
     * 保存药品数据
     *
     * @param standardDrugVo
     * @return
     */
    @Override
    @RpcService
    public Map<String, Object> insertStandardDrug(StandardDrugVo standardDrugVo) {
        Map<String, Object> map = new HashMap<>();
        BaseCurrencyDrug baseCurrencyDrug = new BaseCurrencyDrug();
        ctd.util.BeanUtils.copy(standardDrugVo, baseCurrencyDrug);
        List<BaseDrug> baseDrugList = standardDrugVo.getBaseDrugList();
        baseCurrencyDrug.setPinyinCode(PinyinHelper.getShortPinyin(baseCurrencyDrug.getDrugName()));
        if (0 == baseCurrencyDrug.getDrugInfoId()) {
            baseCurrencyDrugDAO.save(baseCurrencyDrug);
            for (BaseDrug baseDrug : baseDrugList) {
                baseDrug.setDrugInfoId(baseCurrencyDrug.getDrugInfoId());
                if (CommonUtils.isEmpty(baseDrug.getTradeName())) {
                    baseDrug.setTradeName(baseCurrencyDrug.getDrugName());
                }
                baseDrugDAO.save(baseDrug);
            }
        } else {
            baseCurrencyDrugDAO.update(baseCurrencyDrug);

            for (BaseDrug baseDrug : baseDrugList) {
                if (0 == baseDrug.getDrugId()) {
                    if (CommonUtils.isEmpty(baseDrug.getTradeName())) {
                        baseDrug.setTradeName(baseCurrencyDrug.getDrugName());
                    }
                    baseDrugDAO.save(baseDrug);
                } else {
                    baseDrugDAO.update(baseDrug);
                }
            }
        }
        map.put("msg", "保存成功");
        return map;
    }

    /**
     * 通过药品通用表id获取药品信息
     *
     * @param drugInfoId
     * @return
     */
    @RpcService
    @Override
    public StandardDrugVo getStandardDrug(Integer drugInfoId) {
        StandardDrugVo standardDrugVo = new StandardDrugVo();
        BaseCurrencyDrug baseCurrencyDrug = baseCurrencyDrugDAO.get(drugInfoId);
        List<hcn.base.drug.vo.BaseDrugVo> baseDrugs = baseDrugDAO.findDrugList(drugInfoId);
        ctd.util.BeanUtils.copy(baseCurrencyDrug, standardDrugVo);
        if (CommonUtils.isNotEmpty(baseDrugs)) {
            standardDrugVo.setBaseDrugVoList(baseDrugs);
        }
        return standardDrugVo;
    }


    /**
     * 判断药品通用表是否重复
     *
     * @return
     */
    @Override
    @RpcService
    public Map<String, Object> checkBaseCurrencyDrug(String drugName, String dosageFormCode) {
        Map<String, Object> map = new HashMap<>();
        BaseCurrencyDrug baseCurrencyDrug = baseCurrencyDrugDAO.getBaseCurrencyDrug(drugName, dosageFormCode);
        if (CommonUtils.isNotEmpty(baseCurrencyDrug)) {
            map.put("status", -1);
            map.put("msg", "查询数据已存在");
        } else {
            map.put("status", 1);
            map.put("msg", "未查询到相同药品");
        }
        return map;
    }

    /**
     * 返回未被禁用的产地
     *
     * @return
     */
    @RpcService
    public List<BaseFactory> getBaseFactoryList() {
        return baseFactoryDao.findBaseFacetory();
    }
}
